<?php
include("include/config.inc.php");

$pageName = 'incomingEdit';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:index.php");
}
else
{
  $challan          = array();
  $incomingMstArray = 0;
  $incomingDtlArray = 0;
  if(isset($_POST['updateBtn']) && $_POST['incomingId'] > 0)
  {
    $partyId      = isset($_POST['partyId']) ? $_POST['partyId'] : 0;
  	$IncomingDate = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];
  	$qty          = (isset($_POST['qty'])) && ($_POST['qty'] != "") ? $_POST['qty'] : 0 ;
  	$challan      = (isset($_POST['challan'])) && ($_POST['challan'] != "") ? $_POST['challan'] : 0 ;
  	$weight       = (isset($_POST['weight'])) && ($_POST['weight'] != "") ? $_POST['weight'] : 0 ;
  	$processId1   = (isset($_POST['processId1'])) && ($_POST['processId1'] != "") ? $_POST['processId1'] : 0 ;
  	$processId2   = (isset($_POST['processId2'])) && ($_POST['processId2'] != "") ? $_POST['processId2'] : 0 ;
  	$processId3   = (isset($_POST['processId3'])) && ($_POST['processId3'] != "") ? $_POST['processId3'] : 0 ;
  	$material     = (isset($_POST['material'])) && ($_POST['material'] != "") ? $_POST['material'] : 0 ;
  	$hardne       = (isset($_POST['hardne'])) && ($_POST['hardne'] != "") ? $_POST['hardne'] : 0 ;
  	$col1         = (isset($_POST['col1'])) && ($_POST['col1'] != "") ? $_POST['col1'] : NULL ;
  	$col2         = (isset($_POST['col2'])) && ($_POST['col2'] != "") ? $_POST['col2'] : NULL ;
  	$col3         = (isset($_POST['col3'])) && ($_POST['col3'] != "") ? $_POST['col3'] : NULL ;
  	$col4         = (isset($_POST['col4'])) && ($_POST['col4'] != "") ? $_POST['col4'] : NULL ;
  	$col5         = (isset($_POST['col5'])) && ($_POST['col5'] != "") ? $_POST['col5'] : NULL ;
  	$col6         = (isset($_POST['col6'])) && ($_POST['col6'] != "") ? $_POST['col6'] : NULL ;
  
    $updateIncoming = "UPDATE incoming
                          SET partyId = '".$partyId."', IncomingDate = '".$IncomingDate."',
                              qty = '".$qty."',challan = '".$challan."',weight = '".$weight."',
                              processId1 = '".$processId1."',processId2 = '".$processId2."',
                              processId3 = '".$processId3."',materialId = '".$material."',hardne = '".$hardne."',
                              column1 = '".$col1."',column2 = '".$col2."',column3 = '".$col3."',
                              column4 = '".$col4."',column5 = '".$col5."',column6 = '".$col6."'
                        WHERE incomingId = ".$_POST['incomingId'];                         			  
    $updateIncomingResult = mysql_query($updateIncoming);
    if(!$updateIncomingResult)
    {
      die("Update Query Not Updated for Master Table : ".mysql_error());
    }
    else
    {
    	if($processId3 > 0){
    	  $noOfProcess = 3;
    	}
    	else if($processId2 > 0){
    	  $noOfProcess = 2;
    	}
    	else if($processId1 > 0){
    	  $noOfProcess = 1;
    	}
      $updatePlanning = "UPDATE planning 
                            SET partyId = '".$partyId."',quantity = '".$qty."',weight = '".$weight."',
                                processId1 = '".$processId1."',processId2 = '".$processId2."',
                                processId3 = '".$processId3."',noOfProcess = '".$noOfProcess."'
                          WHERE incomingId = ".$_POST['incomingId'];
      $updatePlanningRes = mysql_query($updatePlanning);
      if(!$updatePlanningRes)
      {
      	die("Update Query Not Updated for Planning Table : ".mysql_error()." - ".$updatePlanning);
      }
      else
      {
      	$deleteIncDtl = "DELETE FROM incomingdetail WHERE incomingId = ".$_POST['incomingId'];
        $deleteIncDtlRes = mysql_query($deleteIncDtl);
        $loopCount = 0;                             
        while($loopCount < count($_POST['columns1']))
        {
          $columns1  = ($_POST['columns1'][$loopCount] != '') ? $_POST['columns1'][$loopCount] : NULL;
          $columns2  = ($_POST['columns2'][$loopCount] != '') ? $_POST['columns2'][$loopCount] : NULL;
          $columns3  = ($_POST['columns3'][$loopCount] != '') ? $_POST['columns3'][$loopCount] : NULL;
          $columns4  = ($_POST['columns4'][$loopCount] != '') ? $_POST['columns4'][$loopCount] : NULL;
          $columns5  = ($_POST['columns5'][$loopCount] != '') ? $_POST['columns5'][$loopCount] : NULL;
          $columns6  = ($_POST['columns6'][$loopCount] != '') ? $_POST['columns6'][$loopCount] : NULL;
        
          $insertDetail = "INSERT INTO incomingdetail(incomingId,columns1,columns2,columns3,columns4,columns5,columns6)
                           VALUES(".$_POST['incomingId'].",
                            			 '".$columns1."',
                            			 '".$columns2."',
                            			 '".$columns3."',
                            			 '".$columns4."',
                                   '".$columns5."',
                                   '".$columns6."')";
          $insertDetailRes = mysql_query($insertDetail);
          if(!$insertDetailRes)
          {  
            die("Insert Query Not Inserted for INC Detail : ".mysql_error()."<br>".$insertDetail);
          }
          $loopCount++;
        }
      }
    }
    header('Location:incomingRegister.php');                          
  }
  
  
  if(isset($_GET['incomingId']) && $_GET['incomingId'] > 0)
  {
    $selectincomingRegister = "SELECT *,DATE_FORMAT(incoming.IncomingDate,'%d-%m-%Y')AS IncomingDate
                                 FROM incoming
                                WHERE incomingId = ".$_GET['incomingId'];
    $selectincomingRegisterRes = mysql_query($selectincomingRegister);
    $incomingMstArray = array();
    while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
    {
    	
    	$incomingMstArray['incomingId']   = $incomingRow['incomingId'];
    	$incomingMstArray['partyId']      = $incomingRow['partyId'];
    	$incomingMstArray['IncomingDate'] = $incomingRow['IncomingDate'];
    	$incomingMstArray['qty']          = $incomingRow['qty'];
    	$incomingMstArray['challan']      = $incomingRow['challan'];
    	$incomingMstArray['weight']       = $incomingRow['weight'];
    	$incomingMstArray['processId1']   = $incomingRow['processId1'];
    	$incomingMstArray['processId2']   = $incomingRow['processId2'];
    	$incomingMstArray['processId3']   = $incomingRow['processId3'];
    	$incomingMstArray['materialId']   = $incomingRow['materialId'];
    	$incomingMstArray['hardne']       = $incomingRow['hardne'];
    	$incomingMstArray['column1']      = $incomingRow['column1'];
    	$incomingMstArray['column2']      = $incomingRow['column2'];
    	$incomingMstArray['column3']      = $incomingRow['column3'];
    	$incomingMstArray['column4']      = $incomingRow['column4'];
    	$incomingMstArray['column5']      = $incomingRow['column5'];
    	$incomingMstArray['column6']      = $incomingRow['column6'];
      
      $incDetailData = "SELECT * FROM incomingdetail WHERE IncomingId = ".$incomingRow['incomingId'];
      $incDetailDataRes = mysql_query($incDetailData);
      $incomingDtlArray = array();
      $a = 0;
      while($dtlRow = mysql_fetch_array($incDetailDataRes))
      {
      	$incomingDtlArray[$a]['columns1'] = $dtlRow['columns1'];
      	$incomingDtlArray[$a]['columns2'] = $dtlRow['columns2'];
      	$incomingDtlArray[$a]['columns3'] = $dtlRow['columns3'];
      	$incomingDtlArray[$a]['columns4'] = $dtlRow['columns4'];
      	$incomingDtlArray[$a]['columns5'] = $dtlRow['columns5'];
      	$incomingDtlArray[$a]['columns6'] = $dtlRow['columns6'];
      	$a++;
      }
  
    }
  }
  
	$incomingRegister = array();
	$i =0;
	$selectincomingRegister = "SELECT incomingId,partyName,DATE_FORMAT(incoming.IncomingDate,'%d-%m-%Y')AS IncomingDate,qty,challan,weight,processName1.processName AS processName1,
	                                  processName2.processName AS processName2,processName3.processName AS processName3,materialName,hardne
	                             FROM incoming
	                             LEFT JOIN party ON party.partyId = incoming.partyId
	                             LEFT JOIN process AS processName1 ON processName1.processId = incoming.processId1
	                             LEFT JOIN process AS processName2 ON processName2.processId = incoming.processId2
	                             LEFT JOIN process AS processName3 ON processName3.processId = incoming.processId3
	                             LEFT JOIN material ON material.materialId = incoming.materialId";
	//                            WHERE outgoingDone = '0'   ";
	$selectincomingRegisterRes = mysql_query($selectincomingRegister);
	while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
	{
		$incomingRegister[$i]['incomingId']   = $incomingRow['incomingId'];
		$incomingRegister[$i]['partyName']    = $incomingRow['partyName'];
		$incomingRegister[$i]['IncomingDate'] = $incomingRow['IncomingDate'];
		$incomingRegister[$i]['qty']          = $incomingRow['qty'];
		$incomingRegister[$i]['challan']      = $incomingRow['challan'];
		$incomingRegister[$i]['weight']       = $incomingRow['weight'];
		$incomingRegister[$i]['processName1'] = $incomingRow['processName1'];
		$incomingRegister[$i]['processName2'] = $incomingRow['processName2'];
		$incomingRegister[$i]['processName3'] = $incomingRow['processName3'];
		$incomingRegister[$i]['material']     = $incomingRow['materialName'];
		$incomingRegister[$i]['hardne']       = $incomingRow['hardne'];
		$i++;
	}


	//--------------------------------insert over------------
	$k = 0;
	$catTitleArray = array();
	$select = "SELECT partyId,partyName
	             FROM party
	            ORDER BY partyName";
	$selectResult = mysql_query($select);
	while($row = mysql_fetch_array($selectResult))
	{
	  $catTitleArray['partyId'][$k]   = $row['partyId'];
	  $catTitleArray['partyName'][$k] = $row['partyName'];
	  $k++;
	}
	//-----------------Process-----------------------------------
	 $r = 0;
	 $catprocessArray = array();
	 $select = "SELECT processId,processName                                    
	             FROM process";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catprocessArray['processId'][$r]   = $row['processId'];                    
	  $catprocessArray['processName'][$r] = $row['processName'];                      
	  $r++;                                                                     
	}                                                                           
	  
	//---------------------material---------------------------
  $m = 0;
  $catmaterialArray = array();
  $select = "SELECT materialId,materialName                                    
               FROM material
              ORDER BY materialName";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catmaterialArray['materialId'][$m]   = $row['materialId'];                    
	  $catmaterialArray['materialName'][$m] = $row['materialName'];                      
	  $m++;                                                                     
	}
}
include("./bottom.php");
$smarty->assign("incomingMstArray",$incomingMstArray);
$smarty->assign("incomingDtlArray",$incomingDtlArray);
$smarty->assign("incomingRegister",$incomingRegister);
$smarty->assign("catTitleArray",$catTitleArray);
$smarty->assign("catprocessArray",$catprocessArray);
$smarty->assign("catmaterialArray",$catmaterialArray);
$smarty->assign("msg",'<font size="5">No Record Found</font>');
$smarty->display('incomingEdit.tpl');
?>